<?php
/**
 * Created by PhpStorm.
 * User: chenxj
 * Date: 2018/11/23
 * Time: 15:12
 */

namespace app\wechat\controller;

use think\Config;
use think\Db;
use think\Validate;

class Feedback extends Common
{

    /**
     * 卖家中心问题反馈插入
     */
    public function submitCustomerFeedback()
    {
        $currentCustomer = $this->request->currentCustomer;
        $message = $this->request->post("message");
        $bindPhone = $this->request->post("bindPhone");
        $weChatNumber = $this->request->post("wechatNumber");

        $messageLength = mb_strlen($message);

        if ($messageLength > 150) {
            return errorReturn("errorWechat.errorFeedbackMessageMax");
        }

        if (!pregMobile($bindPhone)) {
            return errorReturn("errorWechat.errorFeedbackPhone");
        }

        // 添加留言
        $messageData['customer_id'] = $currentCustomer['id'];
        $messageData['contact'] = $bindPhone;
        $messageData['content'] = $message;
        $messageData['wechat_number'] = $weChatNumber;

        $this->customerFeedbackService->saveByData($messageData);

        return successReturn();
    }

    /**
     * 店铺商品问题反馈插入
     */
    public function submitGoodsFeedback()
    {
        $currentCustomer = $this->request->currentCustomer;
        $message = $this->request->post("message");
        $bindPhone = $this->request->post("bindPhone");
        $weChatNumber = $this->request->post("wechatNumber");
        $feedbackImages = $this->request->post("feedbackImgs");

        $messageLength = mb_strlen($message);

        if ($messageLength > 150) {
            return errorReturn("errorWeChat.errorFeedbackMessageMax");
        }

        // 添加留言
        $messageData['customer_id'] = $currentCustomer['id'];
        $messageData['contact'] = $bindPhone;
        $messageData['content'] = $message;
        $messageData['wechat_number'] = $weChatNumber;
        $messageData['image_json'] = $feedbackImages;

        $this->goodsFeedbackService->saveByData($messageData);

        return successReturn();
    }

    /**
     * 微信获取手机号加密数据进行解密返回绑定手机号
     */
    public function wechatAuthorizeDecryption()
    {
        $currentCustomer = $this->request->currentCustomer;
        $weChatCode = $this->request->post('wechatCode');
        $encryptedData = $this->request->post('encryptedData');
        $iv = $this->request->post('iv');
        $appId = config("constants.miniProgramConfig.appid");

        // 根据code获取openid
        $authResult = $this->miniProgram->auth->session($weChatCode);

        if (!isset($authResult['session_key'])) {
            return errorReturn('errorWechat.errorAuthCode');
        }

        $sessionKey = $authResult['session_key'];

        $decryptResult = decryptData($sessionKey, $appId,
            $encryptedData, $iv, $weChatResult);

        if (!$decryptResult) {
            return errorReturn('errorWechat.errorAuthCode');
        }
        if (!empty($weChatResult['phoneNumber'])) {
            Db::startTrans();
            try {
                $this->customerService->updateByIdAndData($currentCustomer['id'], ['mobile'=>$weChatResult['phoneNumber']]);
                Db::commit();
            } catch(\PDOException $e) {
                Db::rollback();
                return errorReturn("errorWechat.errorService");
            }
        }
        $returnData = [
            'phoneNumber'=>$weChatResult['phoneNumber']
        ];
        return successReturn($returnData);
    }
}
